require "Mock"

module("LogTest", package.seeall, lunit.testcase)

function test_Log()
	assert_pass(function() Log.trace("trace message") end)
	assert_pass(function() Log.debug("debug message") end)
	assert_pass(function() Log.info("info message") end)
	assert_pass(function() Log.warn("warn message") end)
	assert_pass(function() Log.error("error message") end)
end

module("DatabaseTest", package.seeall, lunit.testcase)

function test_Database()
	assert_true(Database.connect("192.168.136.128", "peeker", "", "tmp"))

	assert_true(Database.insert("example1", { id = 1, key1 = true, key2 = 1, key3 = 0.1, key4 = "string", key5 = {} }, false))

	local status, insertId = Database.insert("example1", { key1 = true, key2 = 1, key3 = 0.1, key4 = "string", key5 = {} }, true)
	assert_true(status)
	assert_not_equal(0, insertId)

	local status, length, rows = Database.select("select * from example1;")
	assert_true(status)
	assert_equal(2, length)

	assert_true(Database.update("example1", { key1 = false }, "id = 1"))
	assert_false(Database.update("example1", { key1 = false }, "id = 1"))

	local status, removed = Database.remove("delete from example1;")
	assert_true(status)
	assert_equal(2, length)
end

module("TemplateTest", package.seeall, lunit.testcase)

function test_Template()
	assert_true(Template.open("../sqlite.db", true))

	local status, length, rows = Template.read("select * from TestTable where id = \"1\" order by id desc;")
	assert_true(status)
	assert_equal(1, length)

	local status, length, rows = Template.read("select * from TestTable where id = @id order by id;", { id = "1" })
	assert_true(status)
	assert_equal(1, length)
end

module("UtilTest", package.seeall, lunit.testcase)

function test_getMiliSeconds()
	local miliSeconds = Util.getMiliSeconds()
	assert_not_equal(0, miliSeconds)
end

function test_md5()
	local hash = Util.md5("aaaaaa")
	assert_equal("0b4e7a0e5fe84ad35fb5f95b9ceeac79", hash)
end

function test_performUrl()
	Util.performUrl("http://www.google.com/")
end

module("AsyncTest", package.seeall, lunit.testcase)

function setup()
	Async.reset()
end

function test_post()
	assert_pass(function() Async.post("callback1", true) end)
	assert_pass(function() Async.post("callback2", true) end)
	assert_pass(function() Async.post("callback3", true) end)

	Async.runOne()
	Async.runAll()
end

function test_join()
	local wrapper = function(callback)
		Async.join(callback, true)
		Log.debug("Resume from " .. callback)
	end

	assert_pass(function() coroutine.wrap(wrapper)("callback1") end)
	assert_pass(function() coroutine.wrap(wrapper)("callback2") end)
	assert_pass(function() coroutine.wrap(wrapper)("callback3") end)

	Async.runOne()
	Async.runAll()
end

module("TimerTest", package.seeall, lunit.testcase)

function setup()
	Timer.reset()
end

function test_setTimeout()
	assert_pass(function() Timer.setTimeout(10, "callback", true) end)
	Timer.run()
end

function test_setInterval()
	assert_pass(function() Timer.setInterval(5, 10, "callback", true) end)
	Timer.run()
end

function test_cancel()
	local timerId1 = Timer.setTimeout(10, "callback", true)
	assert_pass(function() Timer.cancel(timerId1) end)

	local timerId2 = Timer.setInterval(5, 10, "callback", true)
	assert_pass(function() Timer.cancel(timerId2) end)

	Timer.run()
end

module("ClientTest", package.seeall, lunit.testcase)

local player = nil

function test_send()
	assert_pass(function() Client.send(player, "request", true, 1, 0.1, "string") end)
end

module("SceneTest", package.seeall, lunit.testcase)

function test_forward()
	assert_pass(function() Scene.forward(player, "request", true, 1, 0.1, "string") end)
end

function test_multicast()
	assert_pass(function() Scene.multicast(CHANNEL.GRUOP, 1, "request", true, 1, 0.1, "string") end)
end

function test_broadcast()
	assert_pass(function() Scene.broadcast("request", true, 1, 0.1, "string") end)
end

module("WorldTest", package.seeall, lunit.testcase)

function test_call()
	assert_pass(function() World.call("request", true, 1, 0.1, "string") end)
end

function test_broadcast()
	assert_pass(function() World.broadcast("request", true, 1, 0.1, "string") end)
end
